package com.turbo.cloud.ods.web.webservice;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.turbo.cloud.core.annotations.Anonymous;
import com.turbo.cloud.core.api.R;
import com.turbo.cloud.core.api.Result;
import com.turbo.cloud.core.base.PageDynamic;
import com.turbo.cloud.core.base.PageQuery;
import com.turbo.cloud.core.validated.InsertValidated;
import com.turbo.cloud.ods.web.logic.DynamicQueryLogic;
import com.turbo.cloud.ods.web.model.DynamicQueryQo;
import com.turbo.cloud.ods.web.model.DynamicQueryVo;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 动态SQL表 前端控制器
 * </p>
 *
 * @author zhangluning
 */
@RequiredArgsConstructor
@RestController
public class DynamicQueryController {

    final DynamicQueryLogic dynamicQueryLogic;

    /**
     * 动态SQL分页列表
     *
     * @param pageQuery 分页查询
     */
    @Anonymous
    @PostMapping(name = "动态SQL分页列表", value = "/webservice/query/dynamicQueryPage")
    public Result<PageDynamic<Map<String, Object>>> dynamicQueryPage(@RequestBody PageQuery<HashMap<String, String>> pageQuery) {
        PageDynamic<Map<String, Object>> result = dynamicQueryLogic.dynamicQueryPage(pageQuery);
        return R.result(result);
    }

    /**
     * 执行动态SQL分页列表
     *
     * @param pageQuery 分页查询
     */
    @Anonymous
    @PostMapping(name = "执行动态SQL分页列表", value = "/webservice/query/executeDynamicQueryPage")
    public Result<Page<Map<String, Object>>> executeDynamicQueryPage(@RequestBody PageQuery<HashMap<String, String>> pageQuery) {
        Page<Map<String, Object>> result = dynamicQueryLogic.executeDynamicQueryPage(pageQuery);
        return R.result(result);
    }

    /**
     * 解析SQL
     *
     * @param dynamicQueryQo 查询条件
     */
    @Anonymous
    @PostMapping(name = "解析SQL", value = "/webservice/query/analysisSql")
    public Result<List<DynamicQueryVo>> analysisSql(@RequestBody @Validated(InsertValidated.class) DynamicQueryQo dynamicQueryQo) {
        List<DynamicQueryVo> result = dynamicQueryLogic.analysisSql(dynamicQueryQo);
        return R.result(result);
    }
}
